package com.example.ymm.modules1.service.dd;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.query.DbOrderItemQueryCriteria;
import com.example.ymm.modules1.service.user.DbOrderItemService;
import com.example.ymm.util.ServletUtils;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DingMiniNoticeService {

    /**
     * 小程序系统通知
     */

    @Value("${dd.agentId}")
    private Long agentId;

//    @Value("${dd.appId}")
//    String appId;

//    @Value("${dd.corpid}")
//    String corpid;

    @Autowired
    AccessTokenService2 accessTokenService;
    @Autowired
    DbOrderItemService dbOrderItemService;


    String lqfUserId="2363192520970306";//接收消息人员-刘庆峰

    /**
     * 模具费用待办通知OA
     */
    public void OATempleNotice(DbOrder order)  {
        if(UtilValidate.isEmpty(order)){
            return;
        }
        //李翠欢，曾旻銮
        String[] users=new String[]{"024314042226456848","065464065726184593"};
        for(String userId: users){
            sendWaitMsg(order,userId);
        }

    }


    public void sendWaitMsg(DbOrder order,String userId){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(userId);
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FFFF6A00");
        head.setText("请尽快处理");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("商品:");

        String goodName="";
        DbOrderItemQueryCriteria criteria=new DbOrderItemQueryCriteria();
        criteria.setOrderId(order.getOrderId());
        List<DbOrderItem> items = dbOrderItemService.queryAll(criteria);
        if(UtilValidate.isNotEmpty(items)){
            DbOrderItem dbOrderItem = items.get(0);
            goodName= dbOrderItem.getGoodName();
        }
        form1.setValue(goodName);
        OapiMessageCorpconversationAsyncsendV2Request.Form form2 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form2.setKey("兑换时间:");
        form2.setValue(order.getCreateTime().toString());
        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("姓名:");
        form3.setValue(order.getUserName());

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle("积分商品兑换通知");
//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("进行中");
        String url="http://220.161.77.66:10111/#/h5Login?redirect=/ms/mouldList";
        String encode= ServletUtils.urlEncode(url);
//        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
//        String message_url3="dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+agentId+"&redirect_type=jump&redirect_url=h5Login";

        String message_url2="dingtalk://dingtalkclient/page/link?url="+encode+"&pc_slide=true";
        //http://220.161.77.66:10111/#/h5Login
        // oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);

        String accessToken = accessTokenService.getAccessToken();
        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }

    /**
     * 发送积分到账通知
     * @param Ilog
     */
    public void sendIntegralMsg(DbIntegralLog Ilog, DbUserRewards userRewards){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(Ilog.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FFFF6A00");
        head.setText("请查看");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("积分:");
       if(Ilog.getType()==1){
           form1.setValue(Ilog.getIntegral()+"");

       }
        if(Ilog.getType()==2){
            form1.setValue("-"+Ilog.getIntegral()+"");

        }

        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("缘由:");
        form3.setValue(userRewards.getYear()+userRewards.getNote());

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
//        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        if(Ilog.getType()==1){
            body.setTitle("积分到账通知");
        }
        if(Ilog.getType()==2){
            body.setTitle("积分扣除通知");
        }

//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        if(Ilog.getType()==1){
            statusBar.setStatusValue(userRewards.getNote()+"奖励");
        }
        if(Ilog.getType()==2){
            statusBar.setStatusValue(userRewards.getNote()+"处罚");
        }
        String url="http://220.161.77.66:10111/#/h5Login?redirect=/ms/mouldList";
        String encode= ServletUtils.urlEncode(url);
//        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
//        String message_url3="dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+agentId+"&redirect_type=jump&redirect_url=h5Login";

        String message_url2="dingtalk://dingtalkclient/action/open_micro_app?corpId=ding2e2011827f67ec7d35c2f4657eb6378f&agentId=3136542690&miniAppId=5000000006010649&pVersion=1&packageType=1&page=pages/integralList/integralList";
        //http://220.161.77.66:10111/#/h5Login
        // oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        oa.setMessageUrl(message_url2);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);

        String accessToken = accessTokenService.getAccessToken();
        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }


    /**
     * 取消 商城订单退还商品通知
     * @param Ilog
     */
    public void sendIntegralCancelOrderMsg(DbIntegralLog Ilog){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(Ilog.getUserId());
        request.setToAllUser(false);

        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        OapiMessageCorpconversationAsyncsendV2Request.OA oa = new OapiMessageCorpconversationAsyncsendV2Request.OA();

        OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
        head.setBgcolor("FFFF6A00");
        head.setText("请查看");
        OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
        List<OapiMessageCorpconversationAsyncsendV2Request.Form> list = new ArrayList<>();
        OapiMessageCorpconversationAsyncsendV2Request.Form form1 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form1.setKey("积分:");
        if(Ilog.getType()==1){
            form1.setValue(Ilog.getIntegral()+"");

        }
        if(Ilog.getType()==2){
            form1.setValue("-"+Ilog.getIntegral()+"");

        }

        OapiMessageCorpconversationAsyncsendV2Request.Form form3 = new OapiMessageCorpconversationAsyncsendV2Request.Form();
        form3.setKey("缘由:");
        form3.setValue("管理员取消商品退还积分");

//        OapiMessageCorpconversationAsyncsendV2Request.Rich rich = new OapiMessageCorpconversationAsyncsendV2Request.Rich();
//        rich.setNum("200");
//        rich.setUnit("元");

        list.add(form1);
//        list.add(form2);
        list.add(form3);


        body.setForm(list);
//        body.setRich(rich);
        body.setTitle("积分退还通知");

//        body.setImage("@lADOADmaWMzazQKA");
//        body.setFileCount("3");
//        body.setAuthor("小钉");


        OapiMessageCorpconversationAsyncsendV2Request.StatusBar statusBar = new OapiMessageCorpconversationAsyncsendV2Request.StatusBar();
        statusBar.setStatusBg("0xFFFF6A00");
        statusBar.setStatusValue("退还");
        String url="http://220.161.77.66:10111/#/h5Login?redirect=/ms/mouldList";
        String encode= ServletUtils.urlEncode(url);
//        String message_url= "dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+appId+"&redirect_type=jump&redirect_url="+encode;//&redirect_url=/h5Login
//        String message_url3="dingtalk://dingtalkclient/action/openapp?corpid="+corpid+"&container_type=work_platform&app_id="+agentId+"&redirect_type=jump&redirect_url=h5Login";

        String message_url2="dingtalk://dingtalkclient/action/open_micro_app?corpId=ding2e2011827f67ec7d35c2f4657eb6378f&agentId=3136542690&miniAppId=5000000006010649&pVersion=1&packageType=1&page=pages/integralList/integralList";
        //http://220.161.77.66:10111/#/h5Login
        // oa.setMessageUrl(message_url);
        oa.setHead(head);
        oa.setBody(body);
        oa.setStatusBar(statusBar);
        oa.setMessageUrl(message_url2);
        msg.setOa(oa);
        msg.setMsgtype("oa");
        msg.setOa(oa);
        request.setMsg(msg);

        String accessToken = accessTokenService.getAccessToken();
        try{
            OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            System.out.println(rsp.getBody());

        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }


}
